package com.snow.controller.admin;

import com.github.pagehelper.PageInfo;
import com.snow.controller.BaseController;
import com.snow.exception.SiteException;
import com.snow.pojo.dto.cond.CommentCond;
import com.snow.pojo.model.CommentDomain;
import com.snow.service.comment.CommentService;
import com.snow.utils.ApiResponse;
import com.snow.utils.Commons;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.xml.ws.Response;

/**
 * @Description:
 * @author: snow
 * @Date: 2020-01-30
 **/
@Api("评论管理页")
@Controller
@RequestMapping("/admin/comments")
public class CommentController extends BaseController {
    @Autowired
    private Commons commons;
    @Autowired
    private CommentService commentService;
    private static Logger logger = LoggerFactory.getLogger(CommentController.class);
    @GetMapping("")
    @ApiOperation("进入评论列表页")
    public String commentIndex(
            Model model,
            @ApiParam(name = "page", value = "页数", required = false)
            @RequestParam(name = "page", required = false, defaultValue = "1")
                    Integer page,
            @ApiParam(name = "limit", value = "每页的条数", required = false)
            @RequestParam(name = "limit", required = false, defaultValue = "15")
                    Integer limit
    ) {

        PageInfo<CommentDomain> comments = commentService.getCommentsByCond(new CommentCond(), page, limit);
        model.addAttribute("commons", commons);
        model.addAttribute("comments", comments);

        return "admin/comment_list";
    }

    @PostMapping("/delete")
    @ResponseBody
    @ApiOperation("删除评论")
    public ApiResponse deleteComment(
            @ApiParam(name = "coid", value = "评论id", required = true)
            @RequestParam(name = "coid", required = true)
                    Integer coid
    ) {


        try{
            CommentDomain comment = commentService.getCommentByCoid(coid);
            if (comment == null) {
                throw SiteException.withErrorCode("评论不存在！");
            }
            commentService.deleteCommentByCoid(coid);
        }catch (Exception e){
            e.printStackTrace();
            logger.error(e.getMessage());
            return ApiResponse.fail(e.getMessage());
        }
        return ApiResponse.success();
    }

    @ApiOperation("修改评论的状态")
    @PostMapping("updateStatus")
    @ResponseBody
    public ApiResponse updateStatus(
        @ApiParam(name="coid",value="评论id",required = true)
        @RequestParam(name="coid",required = true)
        Integer coid,
        @ApiParam(name="status",value="更新的状态值",required = true)
        @RequestParam(name="status",required = true)
        String status
    ){
        try{
            CommentDomain comment = commentService.getCommentByCoid(coid);
            if(comment == null)
                return ApiResponse.fail("评论不存在！");
            else
                commentService.updateCommentStatus(coid,status);
        }catch(Exception e){
            e.printStackTrace();
            logger.error(e.getMessage());
            return ApiResponse.fail("更新失败！");
        }
        return ApiResponse.success();
    }
}
